<?php
/*
 * 登入校验模块
 * Luocode
 * */
session_start();
echo "<title>登入中...</title>";
$user = $_GET['user'];
$psw =  $_GET['psw'];
$check =  $_GET['check'];

if ($user == "" || $psw =="" ){
    echo "<script>alert('请输入用户名或者密码!');window.history.back();</script>";
    sendmsg("0","未输入用户名或密码");
}else{
    if($user =="admin" && $psw == md5("admin")|| $psw == md5(md5("admin"))){
        $_SESSION['user'] = $user;
        sendmsg("1","登入成功");
        if ($check == "on"){
            $cookiearr = array(
                "user" => $user,
                "psw" => $psw,
                "check" => "checked=''"
            );
            if (!isset($_COOKIE['logincheck'])){
                setcookie("logincheck",serialize($cookiearr),time()+3600);
            }
            }else{
                setcookie("logincheck","",0);
        }
        setcookie("logintoken",time(),time()+60);
        echo "<script>alert('登入成功!');location.href='index.php';</script>";
    }else{
        echo "<script>alert('用户名或者密码错误!');window.history.back();</script>";
        sendmsg("2","用户名或者密码错误");
    }
}

if ($_POST['fun'] == "logout"){
    session_unset();
    session_destroy();
}


function sendmsg($code,$msg){
    $msgarray = array(
        "code" => $code,
        "msg" => $msg
    );
    echo "正在跳转页面...";
    console_log($msgarray);
}


function console_log($data)
{
    if (is_array($data) || is_object($data,JSON_UNESCAPED_UNICODE)) {
        echo("<script>console.log('" . json_encode($data) . "');</script>");
    } else {
        echo("<script>console.log('" . $data . "');</script>");
    }
}